import { Context, Next } from 'koa';
import { requestLogger as logRequest } from '../config/logger';

export const requestLogger = async (ctx: Context & { body: any }, next: Next) => {
  const start = Date.now();

  try {
    await next();
  } finally {
    const duration = Date.now() - start;

    // 使用新的日志系统记录请求
    logRequest(ctx, duration);

    // 记录请求和响应详情（开发环境）
    if (process.env.NODE_ENV === 'development') {
      console.log(`🚀 Request: ${ctx.method} ${ctx.url} - Response Time: ${duration}ms`);
      // console.log('📤 Request Headers:', ctx.request.headers);
      if (ctx.request.body) {
        console.log('📤 Request Body:', ctx.request.body);
      }
      if (ctx.body) {
        ctx.body.responsetime = `${duration}ms`
        console.log('📥 Response:', ctx.body);
      }


    }
  }
};